package org.mobilesynergies.epic.client.parameter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.jivesoftware.smackx.FormField;

public class IntParameter extends Parameter {

	private int mValue = 0;
	private int mMin = Integer.MIN_VALUE;
	private int mMax = Integer.MAX_VALUE;
	private int mStepsize = 1;

	public IntParameter(int value) {
		setValue(value);
	}

	public IntParameter(int value, int min, int max, int stepsize) {
		setMin(min);
		setMax(max);
		setValue(value);
		setStepsize(stepsize);
	}
	
	public IntParameter(HashMap<String, String> map) {
		if(map.containsKey(KEY_VALUE)){
			try{
				mValue = Integer.parseInt(map.get(KEY_VALUE));				
			} catch (Exception e){
				System.out.println("Unable to parse value");
			}
		}
		if(map.containsKey(KEY_MIN)){
			try{
				mMin = Integer.parseInt(map.get(KEY_MIN));				
			} catch (Exception e){
				System.out.println("Unable to parse minimum value");
			}
		}
		if(map.containsKey(KEY_MAX)){
			try{
				mMax = Integer.parseInt(map.get(KEY_MAX));				
			} catch (Exception e){
				System.out.println("Unable to parse maximum value");
			}
		}
		if(map.containsKey(KEY_STEPSIZE)){
			try{
				mStepsize = Integer.parseInt(map.get(KEY_STEPSIZE));				
			} catch (Exception e){
				System.out.println("Unable to parse stepsize");
			}
		}
	}

	/**
	 * @param value the value to set
	 */
	public void setValue(int value) {
		this.mValue = Math.max(Math.min(value, mMax),mMin);
	}

	/**
	 * @return the mValue
	 */
	public int getValue() {
		return mValue;
	}
	
	@Override
	public String getValueAsString() {
		return Integer.toString(getValue());
	}
	
	/**
	 * @param min the min to set
	 */
	public void setMin(int min) {
		this.mMin = min;
	}

	/**
	 * @return the mMin
	 */
	public int getMin() {
		return mMin;
	}

	/**
	 * @param max the max to set
	 */
	public void setMax(int max) {
		this.mMax = max;
	}

	/**
	 * @return the mMax
	 */
	public int getMax() {
		return mMax;
	}

	/**
	 * @param stepsize the stepsize to set
	 */
	public void setStepsize(int stepsize) {
		this.mStepsize = stepsize;
	}

	/**
	 * @return the mStepsize
	 */
	public int getStepsize() {
		return mStepsize;
	}

	@Override
	public String getType() {
		return Parameter.TYPENAME_INT;
	}
	
	@Override
	public String toXml(String name) {
		String type = Integer.class.getCanonicalName();
		String value = Integer.toString(mValue); 
		String extraattributes = null;
		String extraxml = null;
		return toXml(name, type, value, extraattributes, extraxml);
	}
	
	@Override
	public Set<FormField> toFormFields(String name) {
		Set<FormField> set = new HashSet<FormField>();
		FormField f1 = new FormField(name);
		f1.setLabel(name);
		f1.setType(FormField.TYPE_TEXT_SINGLE);
		f1.addValue(Integer.toString(mValue));
		set.add(f1);
		
		FormField f2 = new FormField(name+SPECIALIZECHARACTER+KEY_MIN);
		f2.setLabel(name);
		f2.setType(FormField.TYPE_HIDDEN);
		f2.addValue(Integer.toString(mMin));
		set.add(f2);
		
		FormField f3 = new FormField(name+SPECIALIZECHARACTER+KEY_MAX);
		f3.setLabel(name);
		f3.setType(FormField.TYPE_HIDDEN);
		f3.addValue(Integer.toString(mMax));
		set.add(f3);
		
		FormField f4 = new FormField(name+SPECIALIZECHARACTER+KEY_STEPSIZE);
		f4.setLabel(name);
		f4.setType(FormField.TYPE_HIDDEN);
		f4.addValue(Integer.toString(mStepsize));
		set.add(f4);
		
		FormField f5 = new FormField(name+SPECIALIZECHARACTER+KEY_TYPE);
		f5.setLabel(name);
		f5.setType(FormField.TYPE_HIDDEN);
		f5.addValue(TYPENAME_INT);
		set.add(f5);		
		
		return set;
	}


}
